package GUI.view;

import java.awt.Component;
import java.util.ArrayList;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;

/**
 * 
 * @author Wei WANG
 * 
 */

public class MultiLineTable extends JTable {

	private int indexSubTable;
	ArrayList<JTable> subTables;
	TableModel mainTableModel;
	boolean titleVisible;
	private int tableHeight;

	public MultiLineTable(TableModel mainTableModel, ArrayList<JTable> subTables,
			int indexSubTable, boolean titleVisible) {
		this.indexSubTable = indexSubTable;
		this.subTables = subTables;
		this.mainTableModel = mainTableModel;
		this.titleVisible = titleVisible;
		setModel(mainTableModel);
	}

	public void setTableHeight(int tableHeight) {
		this.tableHeight = tableHeight;
	}

	public int getTableHeight() {
		return this.tableHeight + 24;
	}

	public TableCellRenderer getCellRenderer(int row, int column) {
		if (column == indexSubTable && row < subTables.size()) {
			return new TableCellRenderer() {
				public Component getTableCellRendererComponent(JTable table,
						Object value, boolean isSelected, boolean hasFocus,
						int row, int column) {
					if (titleVisible) {
						JScrollPane scrollPanel = new JScrollPane(
								subTables.get(row));
						scrollPanel
								.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
						return scrollPanel;
					}
					return subTables.get(row);
				}
			};
		} else
			return super.getCellRenderer(row, column);
	}

}
